<template>
  <BasicTable @register="registerTable" @resize-column="handleResizeColumn">
    <template #toolbar> </template>
    <template #action="{ record }">
      <TableAction
        :actions="[
          {
            icon: 'ant-design:rollback-outlined',
            tooltip: '恢复',
            popConfirm: {
              title: '是否确认恢复',
              placement: 'leftBottom',
              confirm: handleRecover.bind(null, record),
            },
          },
          {
            icon: 'ant-design:delete-outlined',
            color: 'error',
            popConfirm: {
              title: '是否确认删除',
              placement: 'leftBottom',
              confirm: handleDelete.bind(null, record),
            },
          },
        ]"
      />
    </template>
  </BasicTable>
</template>
<script lang="ts">
  import { defineComponent, onMounted, ref, nextTick } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { searchFormSchema, firstColumns } from './moduleHisList.data';
  import { useTabs } from '/@/hooks/web/useTabs';
  import { useRouter } from 'vue-router';
  import { getFormGetFields } from '/@/api/flowManage/flowManage';
  import {
    getAdminHistoryList,
    getAdminHistoryDel,
    getAdminHistoryRestore,
  } from '/@/api/module/module';
  import { useMessage } from '/@/hooks/web/useMessage';

  export default defineComponent({
    name: 'ModuleHisList',
    components: { BasicTable, TableAction },
    setup() {
      const { createMessage } = useMessage();
      const { setTitle } = useTabs();
      const { currentRoute } = useRouter();
      const formCode = ref('');
      const title = '历史记录';
      onMounted(async () => {
        if (currentRoute.value?.query?.title) {
          const newTitle = `${title}-` + currentRoute.value?.query?.title;
          setTitle(`${newTitle}`);
          setProps({ title: newTitle });
        }
      });

      const [registerTable, { reload, setProps, setColumns, setPagination }] = useTable({
        title,
        api: getAdminHistoryList,
        columns: [],
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        searchInfo: {
          op: 'search',
        }, //额外的参数
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        indexColumnProps: { width: 50 },
        showIndexColumn: true,
        beforeFetch: (info) => {
          let newInfo = info;
          if (newInfo.dates && newInfo.dates.length > 0) {
            newInfo.beginDate = newInfo.dates[0];
            newInfo.endDate = newInfo.dates[1];
            delete newInfo.dates;
          }
          newInfo.formCode = currentRoute.value.query.formCode;
          newInfo.fdaoId = currentRoute.value.query.id;
          return newInfo;
        },
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });

      async function handleDelete(record: Recordable) {
        await getAdminHistoryDel({ id: record.id, formCode: formCode.value }).then(() => {
          handleSuccess();
          createMessage.success('操作成功');
        });
      }

      //获取表头数据
      const columns = ref<Recordable[]>([]);
      const getFormGetFieldsData = () => {
        columns.value = [...firstColumns];
        try {
          if (!formCode.value) return;
          getFormGetFields({ formCode: formCode.value }).then((res) => {
            const data = res || [];
            if (data && data.length) {
              data.forEach((item: Recordable) => {
                columns.value.push({
                  width: 100,
                  title: item.title,
                  dataIndex: item.name,
                  customCell: (record) => {
                    if (!record[`${item.name}_isSame`]) {
                      return {
                        style: {
                          backgroundColor: '#FFFF66',
                        },
                      };
                    }
                  },
                });
              });
            }
            setColumns(columns.value);
          });
        } catch (err) {
          console.error('getFormGetFields', err);
        }
      };

      function handleSuccess() {
        reload();
      }
      onMounted(async () => {
        await nextTick();
        setPagination({
          pageSize: 50,
        });
        formCode.value = currentRoute.value.query.formCode as any;
        getFormGetFieldsData();
      });

      //恢复
      const handleRecover = async (record: Recordable) => {
        await getAdminHistoryRestore({ id: record.id, formCode: formCode.value }).then(() => {
          handleSuccess();
          createMessage.success('操作成功');
        });
      };
      return {
        registerTable,
        handleDelete,
        handleRecover,
        handleSuccess,
        handleResizeColumn: (w, col) => {
          col.width = w;
        },
      };
    },
  });
</script>
